/*
 *  opengl_main.cpp
 *  framework
 *
 *  Created by gerhard gubler on 17.11.09.
 *  Copyright 2009 md80.ch. All rights reserved.
 *
 */

#include "opengl_main.h"
#include <unistd.h>
#include <string>
#include <iostream>

using std::string;
using std::cout;
using std::endl;

float opengl_main::val;
midpanel opengl_main::mymidpanel;
string opengl_main::myvalue;
datavalues opengl_main::dvals;
PrimaryFlightDisplay opengl_main::pfd_disp;
int opengl_main::window1;
int opengl_main::window2;
int opengl_main::window3;

opengl_main::opengl_main()
{
	
}

extern "C" void display()
{
	opengl_main::caller();
}

extern "C" void pfd_display()
{
	opengl_main::pfd_caller();
}

extern "C" void keyboard(unsigned char key, int x, int y)
{
switch (key) 
	{
	case 'd':
		opengl_main::incr();
		break;
	case 'e':
		opengl_main::decr();
		break;
	case 's':
		opengl_main::startup();
	break;
	case 'c':
		opengl_main::shutdown();
		break;
	
	default:
		break;
	}
}

extern "C" void reshapeM(int w, int h)
{
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0, w, 0, h, -1.0, 1.0);
	glMatrixMode(GL_MODELVIEW);
}

extern "C" void reshapePFD(int w, int h)
{
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0, w, 0, h, -1.0, 1.0);
	glMatrixMode(GL_MODELVIEW);
}

void opengl_main::getnetdata()
{
	while(true)
	{
		int pos = 0;
		myvalue = mycon.get();
		cout << "MYVALUE :" << myvalue << endl;
		string ddat = myvalue.substr(0, 4);
		dvals.n1_L = atof(ddat.c_str());
		
		pos = myvalue.find(";", pos+1);
		ddat = myvalue.substr(pos+1, 4);
		dvals.n1_R = atof(ddat.c_str());
		
		pos = myvalue.find(";", pos+1);
		ddat = myvalue.substr(pos+1, 4);
		dvals.n2_L = atof(ddat.c_str());
		
		pos = myvalue.find(";", pos+1);
		ddat = myvalue.substr(pos+1, 4);
		dvals.n2_R = atof(ddat.c_str());
		
		pos = myvalue.find(";", pos+1);
		ddat = myvalue.substr(pos+1, 5);
		dvals.egt_L = atof(ddat.c_str());
		
		pos = myvalue.find(";", pos+1);
		ddat = myvalue.substr(pos+1, 5);
		dvals.egt_R = atof(ddat.c_str());
		
		//usleep(10000);
	}
}


void opengl_main::incr()
{
	val = val + 0.1;
}

void opengl_main::decr()
{
	val = val - 0.1;
}

void opengl_main::get_going()
{
	int argc = 0;
	char **argv = 0;
	window1 = 0;
	window2 = 0;
	window3 = 0;
	glutInit(&argc, argv);
	
	mycon.listener();
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(800, 600);
	glutInitWindowPosition(10, 100);
	window1 = glutCreateWindow("Engine Panel");
	glutDisplayFunc(display);
	glutKeyboardFunc(keyboard);
	glutReshapeFunc(reshapeM);

	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	 
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);             // Pixel Storage Mode To Byte Alignment (NEW)
    glEnable(GL_POINT_SMOOTH);
    glEnable(GL_LINE_SMOOTH);
	
    glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
    glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
	
	glShadeModel(GL_SMOOTH);
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glViewport(0.0, 0.0, 800, 600);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0, 800, 0, 600, -1.0, 1.0);
	glMatrixMode(GL_MODELVIEW);
	mymidpanel.init();
	
	boost::thread j1(boost::bind(&opengl_main::getnetdata, this));

	
	window2 = glutCreateWindow("Primary Flight Display");
	glutKeyboardFunc(keyboard);
	glutReshapeFunc(reshapePFD);	
	glutPositionWindow(820, 100);
	glutDisplayFunc(pfd_display);

	glutMainLoop();
}

void opengl_main::caller()
{
	//usleep(10000);
	glutSetWindow(window1);
	glClear(GL_COLOR_BUFFER_BIT);	// Clear Screen And Depth Buffer
	glLoadIdentity();
	mymidpanel.seteprLval(val * 10);
	mymidpanel.seteprRval(val * 10);
	mymidpanel.setegtLval(dvals.egt_L);
	mymidpanel.setegtRval(dvals.egt_R);
	mymidpanel.setn1Lval(dvals.n1_L);
	mymidpanel.setn1Rval(dvals.n1_R);
	mymidpanel.setn2Lval(dvals.n2_L);
	mymidpanel.setn2Rval(dvals.n2_R);
	mymidpanel.display();
	glFlush();
	glutSwapBuffers();
	glutPostRedisplay();
//	cout << "VAL :" << val << " myvalue :" << myvalue << endl;
}

void opengl_main::pfd_caller()
{
	glutSetWindow(window2);
	//glClearColor(1.0, 0.0, 0.0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glLoadIdentity();
	pfd_disp.display();
	glFlush();
	glutSwapBuffers();
	glutPostRedisplay();
}

void opengl_main::startup()
{
	while(val < 200)
	{
		usleep(10000);
		val = val + 0.1;
		pfd_disp.pos = pfd_disp.pos + 0.1;
		caller();
		pfd_caller();
	}
}

void opengl_main::shutdown()
{
	while(val > 0)
	{
		usleep(10000);
		val = val - 0.1;
		caller();
		pfd_caller();
	}	
}

